using System;
using System.Collections.Generic;
using System.Text;
using NBusiness.Properties;
using System.Runtime.Serialization;
using System.Collections.ObjectModel;
using System.Security.Permissions;

namespace NBusiness.Data.Validation
{
    [Serializable]
    public class ValidationException : Exception
    {
        EntityBase _entity;

        public EntityBase Entity
        {
            get { return _entity; }
        }

        Collection<ValidationRule> _validationRules = new Collection<ValidationRule>();

        public Collection<ValidationRule> BrokenRules
        {
            get { return _validationRules; }
        }

        public ValidationException() { }

        public ValidationException(string message)
            : base(message)
        { }

        public ValidationException(string message, Exception innerException)
            : base(message, innerException)
        { }

        public ValidationException(EntityBase entity, Collection<ValidationRule> brokenRules, string message)
            : base(message)
        {
            _entity = entity;
            _validationRules = brokenRules;
        }

        protected ValidationException(SerializationInfo info, StreamingContext context) 
            : base(info, context)
        { }

        [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new ArgumentNullException(Resources.ValidationExceptionInfoParam, Resources.ValidationExceptionInfoNullMessage);

            info.AddValue("_validationRules", _validationRules);
            info.AddValue("_entity", _entity);
            base.GetObjectData(info, context);
        }
    }
}
